package com.tencent.wxcloudrun.utils;

//import org.apache.commons.lang3.StringUtils;
import cn.hutool.core.util.StrUtil;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

public class WebUtils {
    public WebUtils() {
    }

    public static String getRealIP(HttpServletRequest request) {
        String xffAddr = request.getHeader("X-Forwarded-For");
        if (StrUtil.isNotBlank(xffAddr)) {
            int index = xffAddr.indexOf(",");
            if (index > 0) {
                return xffAddr.substring(0, index);
            }
        }

        return request.getRemoteAddr();
    }

    public static String getRequestURI(HttpServletRequest request) {
        String realUri = request.getHeader("X-Real-Uri");
        if (StrUtil.isNotBlank(realUri)) {
            int index = realUri.indexOf("?");
            if (index > 0) {
                realUri = realUri.substring(0, index);
            }

            return realUri;
        } else {
            return request.getRequestURI() != null ? request.getRequestURI() : "";
        }
    }

    public static String describeRequest(HttpServletRequest request) {
        return "request type:" + request.getClass().getName() + ", request headers:" + describeRequestHeaders(request) + ", request cookies:" + describeRequestCookies(request) + ", request content type:" + request.getContentType();
    }

    public static String describeRequestHeaders(HttpServletRequest request) {
        StringBuilder builder = new StringBuilder("[");
        Enumeration<String> headerNames = request.getHeaderNames();

        while(headerNames != null && headerNames.hasMoreElements()) {
            String headerName = (String)headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            builder.append(headerName).append("=").append(headerValue).append(",");
        }

        builder.append("]");
        return builder.toString();
    }

    public static String describeRequestCookies(HttpServletRequest request) {
        StringBuilder builder = new StringBuilder("[");
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            Cookie[] var3 = cookies;
            int var4 = cookies.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Cookie cookie = var3[var5];
                builder.append(cookie.getName()).append("=").append(cookie.getValue()).append(",");
            }
        }

        builder.append("]");
        return builder.toString();
    }

    public static String describeRequestParameters(HttpServletRequest request) {
        return (String)request.getParameterMap().entrySet().stream().map((entry) -> {
            return (String)entry.getKey() + ":" + Arrays.toString((Object[])entry.getValue());
        }).collect(Collectors.joining(", "));
    }

//    public static String describeAccessLog(HttpServletRequest request, HttpServletResponse response) {
//        String uri = request.getRequestURI() == null ? "" : request.getRequestURI();
//        return String.format("%s %s - - \"%s %s %s\" %d %d %s %s", Thread.currentThread().getName(), getRealIP(request), request.getMethod(), request.getQueryString() == null ? uri : uri + "?" + request.getQueryString(), request.getProtocol(), response.getStatus(), System.currentTimeMillis() - TimeHolder.get(), request.getHeader("Referer"), request.getHeader("User-Agent"));
//    }

//    public static List<String> safeSplit(String str, String separatorChars) {
//        if (StrUtil.isBlank(str)) {
//            return new ArrayList();
//        } else {
//            String[] split = StrUtil.split(str, separatorChars);
//            List<String> list = new ArrayList();
//            String[] var4 = split;
//            int var5 = split.length;
//
//            for(int var6 = 0; var6 < var5; ++var6) {
//                String s = var4[var6];
//                if (StringUtils.isNotBlank(s)) {
//                    list.add(s);
//                }
//            }
//
//            return list;
//        }
//    }

    public static String describeResponse(HttpServletResponse response) {
        Collection<String> headerNames = response.getHeaderNames();
        if (CollectionUtils.isEmpty(headerNames)) {
            return "null";
        } else {
            StringBuilder builder = new StringBuilder("[");
            Iterator var3 = headerNames.iterator();

            while(var3.hasNext()) {
                String headerName = (String)var3.next();
                String headerValue = response.getHeader(headerName);
                builder.append(headerName).append("=").append(headerValue).append(",");
            }

            builder.append("]");
            return builder.toString();
        }
    }
}